package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.service.TestService;
import io.swagger.models.auth.In;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: lx
 * @Time: 14:14
 * @Description:
 */
@Service
public class TestServiceImpl implements TestService {

    //JUC
    private Lock lock = new ReentrantLock();
    @Autowired
    private RedisTemplate redisTemplate;
    //syn  lock syn(this)
    @Override
    public  void lock() {// 5000个线程
        //分布式锁
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("num123", "7i7");
        if(aBoolean){
            try {
                Integer num = (Integer) redisTemplate.opsForValue().get("num");
                num++;
                redisTemplate.opsForValue().set("num",num);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        }else{
            lock();
        }
    }
}
